<?php

/**
 * Description of Laboratorio
 *
 * @author markos
 */
class VirtualLabs_Academico_Laboratorio {

    const TABLE_NAME = 'laboratorio';
    const ROL_PROFESOR = 2;
    const ROL_ADMIN = 3;
    const ROL_ALUMNO = 1;
    const ACTIVO = 'A';
    const INACTIVO = 'I';

    /**
     * Retorna todos los laboratorios para un $username.
     * 
     * @param type $username
     * @return type
     * @throws Exception
     */
    public function getMachinesByLab($lab) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()->from('maquina_virtual')->where('codigo_laboratorio = ?', $lab);
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }

    public function getMachinesByUsername($username) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()
                    ->from(array('mv' => 'maquina_virtual'), '*')
                    ->join(array('mvu' => 'maquina_virtual_usuario'), "mv.codigo_maquina_virtual = mvu.codigo_maquina_virtual", array())
                    ->where('mvu.username = ?', $username);
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * Obtener los datos de los laboratorios vinculados al username
     * @param type $username
     */
    public function getLabsByUsername($username) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()
                    ->from(array('la' => 'laboratorio'))
                    ->join(array('ml' => 'miembro_laboratorio'), 'la.codigo_laboratorio = ml.codigo_laboratorio')
                    ->join(array('aa' => 'area_academica'), 'aa.codigo_area_academica = la.codigo_area_academica', array('nombre_area_academica' => 'aa.nombre'))
                    ->where('ml.username = ?', $username);
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }
    
    /**
     * Obtener los datos de los laboratorios vinculados al username
     * @param type $username
     */
    public function getLabsByUsernameAndCodLab($username, $codigoLaboratorio) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()
                    ->from(array('ml' => 'miembro_laboratorio'))                    
                    ->where('ml.username = ?', $username)
                    ->where('ml.codigo_laboratorio = ?', $codigoLaboratorio);
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * Obtener los datos de los laboratorios vinculados al username
     * @param type $username
     */
    public function getProfesoresByLab($codigo_laboratorio) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()
                    ->from(array('ml' => 'miembro_laboratorio'))
                    ->join(array('u' => 'usuario'), 'u.username = ml.username', array('nombre' => 'u.nombre'))
                    ->where('ml.codigo_laboratorio = ?', $codigo_laboratorio)
                    ->where('u.tipo_usuario = ?', VirtualLabs_Academico_Laboratorio::ROL_PROFESOR);
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }
    
    public function getVMyLabUsername($cod_lab,$username) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            $select = $db->select()
                    ->from(array('mv' => 'maquina_virtual'))                    
                    ->join(array('mvu' => 'maquina_virtual_usuario'), 'mvu.codigo_maquina_virtual = mv.codigo_maquina_virtual', array('codigo_maquina_virtual' => 'mvu.codigo_maquina_virtual'))
                    ->where('mv.codigo_laboratorio = ?', $cod_lab)
                    ->where('mvu.username = ?', $username);
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug("[ VMMyLab ] $select");            
            return $db->fetchAll($select);
        } catch (Exception $e) {
            throw $e;
        }
    }
    
    /**
     * 
     * @param type $cod_matricula
     * @return type
     * @throws Exception
     */
    public function getLabByCodMatricula($cod_matricula) {
        $db = Zend_Db_Table::getDefaultAdapter();
        try {
            //$select = $db->select()->from(self::TABLE_NAME)->where('codigo_matricula = ?', $cod_matricula);
            $select = $db->select()->from(array('lab' => self::TABLE_NAME))
                    ->join(array('pe' => 'perfil_encendido'), 'pe.codigo_perfil_encendido = lab.codigo_perfil_encendido')
                    ->where('lab.codigo_matricula = ?', $cod_matricula);
            return $db->fetchAll($select);            
        } catch (Exception $e) {
            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug($e->getMessage());            
            $debug->debug($e->getTraceAsString());            
            throw $e;
        }
    }

}

?>
